package com.leadership.api.service.impl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.JsonUtils;
import com.leadership.api.dao.IdmDao;
import com.leadership.api.dao.IdmUserDao;
import com.leadership.api.handler.IdmHandler;
import com.leadership.api.handler.UMUHandler;
import com.leadership.api.rao.SurveyRao;
import com.leadership.api.service.IdmService;
import com.leadership.entity.LearningMap;
import com.leadership.entity.LearningMapStudent;
import com.leadership.entity.idm.IdmOrg;
import com.leadership.entity.idm.IdmSyncLog;
import com.leadership.entity.idm.IdmUser;
import com.leadership.entity.idm.Organization;
import com.leadership.entity.umu.UMUCourseStat;

/**
 * 
 * @author dan
 *
 */
@Service("idmService")
public class IdmServiceImpl implements IdmService {

    private static final Logger logger = LoggerFactory.getLogger(IdmServiceImpl.class);

    @Dao
    private IdmDao idmDao;

    @Dao
    private IdmUserDao idmUserDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Autowired
    private IdmHandler idmHandler;

    @Autowired
    private UMUHandler umuHandler;

    @Rao
    private SurveyRao surveyRao;

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#listIdmUser()
     */
    public Map<String, List<IdmUser>> listIdmUser() {
        List<IdmUser> idmUsers = idmUserDao.listAllIdmUser();
        return CollectionUtils.constructMapValues(idmUsers, "parentOrgnofullpath");
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#listIdmOrganization()
     */
    public List<Organization> listIdmOrganization() {
        return genericDaoSupport.loadAll(Organization.class);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#updateOrg()
     */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult updateOrg() {

        HttpResult result = new HttpResult();

        try {
            // 1. delete from idm_org
            genericDaoSupport.executeHQL("DELETE FROM IdmOrg", IdmOrg.class);

            // 2. select all idm_org where orgstatus = 1
            String encode = URLEncoder.encode("1", "UTF-8");
            String filter = "(orgstatus=" + encode + ")";
            List<IdmOrg> idmOrgs = idmHandler.listIdmOrg(filter);

            // 3. list all organization
            List<Organization> organizations = genericDaoSupport.loadAll(Organization.class);

            if (idmOrgs != null && !idmOrgs.isEmpty()) {

                for (IdmOrg idmOrg : idmOrgs) {

                    for (Organization organization : organizations) {
                        if (StringUtils.equals(idmOrg.getOrgnofullpath(), organization.getOrgnofullpath())) {
                            // 4. update organization
                            genericDaoSupport.update(organization.edit(idmOrg));

                            // 5. update idm_org set is_used
                            idmOrg.editUsed(true);
                        }
                    }

                    // 6. insert into idm_org
                    genericDaoSupport.save(idmOrg);
                }

            } else {
                result.fail("500").message("get idm_org failed");
                return result;
            }

            result.success().message("update org success");

        } catch (UnsupportedEncodingException e) {
            logger.error("#updateOrg error - ", e);
            result.fail("500").message("update org failed");
        }
        return result;
    }

    public void updateNewUser(List<IdmUser> idmUsers) {
        // logger.info("get idmUsers[{}]", JsonUtils.toJson(idmUsers));
        if (idmUsers != null && !idmUsers.isEmpty()) {

            for (IdmUser idmUser : idmUsers) {
                if (idmUser.getMail() != null) {
                    IdmUser old = idmUserDao.load(idmUser.getMail());
                    if (old != null) {
                        logger.info("current old user is [{}]", idmUser.getMail());
                        // genericDaoSupport.update(old.update(idmUser));
                    } else {
                        // 5. initilization idm_user where userstatus = 1
                        if (idmUser.getUsertype() != null && (idmUser.getUsertype().equalsIgnoreCase("E1") || idmUser.getUsertype().equalsIgnoreCase("E2"))) {
                            idmUser.editOrgnofullpath(idmUser.getOrgParent());
                            genericDaoSupport.save(idmUser);
                            logger.info("current new user is [{}]", idmUser.getMail());
                            logger.info("current new user is [{}]", JsonUtils.toJson(idmUser));

                            String yesterday = DateFormatUtils.format(DateUtils.addDays(new Date(), -1), "yyyyMMdd") + "000000Z";
                            String joinTime = idmUser.getJoinsysdate();
                            logger.info("yesterday is [{}] joinTime is [{}]", yesterday, joinTime);
                            if (joinTime != null) {
                                if (joinTime.compareTo(yesterday) >= 0) {
                                    Date date = null;
                                    logger.info("current new user duty is [{}]", idmUser.getDutyid());
                                    if (StringUtils.isNotEmpty(idmUser.getDutyid())) {
                                        BigDecimal duity = null;
                                        try {
                                            duity = new BigDecimal(idmUser.getDutyid());
                                        } catch (Exception e) {
                                            logger.info("current new user duty is not number, ignore here", idmUser.getDutyid());
                                            continue;
                                        }
                                        if (duity.compareTo(new BigDecimal("6.0")) < 0) {
                                            date = new Date();
                                            date = DateUtils.addMonths(date, 3);
                                        } else {
                                            date = new Date();
                                            date = DateUtils.addMonths(date, 6);
                                        }
                                    }

                                    logger.info("current new user parent org is [{}]", idmUser.getParentOrgnofullpath());
                                }
                            }
                        }
                    }
                }

            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#updateUser()
     */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult updateAllUser() {
        HttpResult result = new HttpResult();

        try {

            // 1. delete from idm_user
            // genericDaoSupport.executeHQL("DELETE FROM IdmUser", IdmUser.class);

            // 2. list all organization
            List<Organization> organizations = genericDaoSupport.loadAll(Organization.class);
            for (Organization organization : organizations) {

                // 3. get child idm_org
                String orgnofullpath = organization.getOrgnofullpath() + "%";
                List<IdmOrg> idmOrgs = idmDao.listIdmOrg(orgnofullpath);
                for (IdmOrg idmOrg : idmOrgs) {
                    // 4. get user by orgnofullpath
                    String encode = URLEncoder.encode(idmOrg.getOrgnofullpath(), "UTF-8");
                    String filter = "(orgnofullpath=" + encode + ")";
                    List<IdmUser> idmUsers = idmHandler.listIdmUser(filter);
                    // logger.info("get idm Org is [{}]", idmOrg.getOrgnamefullpath());
                    // logger.info("get idmUsers[{}]", JsonUtils.toJson(idmUsers));
                    if (idmUsers != null && !idmUsers.isEmpty()) {

                        for (IdmUser idmUser : idmUsers) {
                            if (idmUser.getMail() != null) {
                                IdmUser old = idmUserDao.load(idmUser.getMail());
                                if (old != null) {
                                    // logger.info("current old user is [{}]", idmUser.getMail());
                                    genericDaoSupport.update(old.update(idmUser));
                                } else {
                                    // 5. initilization idm_user where userstatus = 1
                                    if (idmUser.getUsertype() != null && (idmUser.getUsertype().trim().equalsIgnoreCase("E1") || idmUser.getUsertype().trim().equalsIgnoreCase("E2"))) {

                                        idmUser.editOrgnofullpath(idmUser.getOrgParent());
                                        genericDaoSupport.save(idmUser);
                                        IdmSyncLog idmSyncLog = new IdmSyncLog(idmUser, true, "同步成功", JsonUtils.toJson(idmUser));
                                        genericDaoSupport.save(idmSyncLog);
                                        logger.info("current new user is [{}]", idmUser.getMail());
                                        // logger.info("current new user is [{}]",
                                        // JsonUtils.toJson(idmUser));

                                        Date date = null;
                                        logger.info("current new user duty is [{}]", idmUser.getDutyid());
                                        if (StringUtils.isNotEmpty(idmUser.getDutyid())) {
                                            BigDecimal duity = null;
                                            try {
                                                duity = new BigDecimal(idmUser.getDutyid());
                                            } catch (Exception e) {
                                                logger.info("current new user duty is not number, ignore here", idmUser.getDutyid());
                                                continue;
                                            }
                                            if (duity.compareTo(new BigDecimal("6.0")) < 0) {
                                                date = new Date();
                                                date = DateUtils.addMonths(date, 3);
                                            } else {
                                                date = new Date();
                                                date = DateUtils.addMonths(date, 6);
                                            }
                                        }

                                        logger.info("current new user parent org is [{}]", idmUser.getParentOrgnofullpath());
                                        if (idmUser.getParentOrgnofullpath() != null) {
                                            if (organization != null) {
                                                LearningMap learningMap1 = genericDaoSupport.load(LearningMap.class, new Long(1));
                                                LearningMapStudent learningMapStudent1 = new LearningMapStudent(idmUser, organization, learningMap1, date, true);
                                                genericDaoSupport.save(learningMapStudent1);

                                                String job = idmUser.getZ9121znt();
                                                if (StringUtils.isNotBlank(job)) {
                                                    if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("财务")) {
                                                        LearningMap learningMap8 = genericDaoSupport.load(LearningMap.class, new Long(8));
                                                        LearningMapStudent learningMapStudent8 = new LearningMapStudent(idmUser, organization, learningMap8, date, true);
                                                        genericDaoSupport.save(learningMapStudent8);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("采购")) {
                                                        LearningMap learningMap6 = genericDaoSupport.load(LearningMap.class, new Long(6));
                                                        LearningMapStudent learningMapStudent6 = new LearningMapStudent(idmUser, organization, learningMap6, date, true);
                                                        genericDaoSupport.save(learningMapStudent6);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("成本")) {
                                                        LearningMap learningMap7 = genericDaoSupport.load(LearningMap.class, new Long(7));
                                                        LearningMapStudent learningMapStudent7 = new LearningMapStudent(idmUser, organization, learningMap7, date, true);
                                                        genericDaoSupport.save(learningMapStudent7);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("法务")) {
                                                        LearningMap learningMap11 = genericDaoSupport.load(LearningMap.class, new Long(11));
                                                        LearningMapStudent learningMapStudent11 = new LearningMapStudent(idmUser, organization, learningMap11, date, true);
                                                        genericDaoSupport.save(learningMapStudent11);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("工程")) {
                                                        LearningMap learningMap5 = genericDaoSupport.load(LearningMap.class, new Long(5));
                                                        LearningMapStudent learningMapStudent5 = new LearningMapStudent(idmUser, organization, learningMap5, date, true);
                                                        genericDaoSupport.save(learningMapStudent5);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("行政") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("人力资源") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("学院")) {
                                                        LearningMap learningMap9 = genericDaoSupport.load(LearningMap.class, new Long(9));
                                                        LearningMapStudent learningMapStudent9 = new LearningMapStudent(idmUser, organization, learningMap9, date, true);
                                                        genericDaoSupport.save(learningMapStudent9);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("客户关系")) {
                                                        LearningMap learningMap10 = genericDaoSupport.load(LearningMap.class, new Long(10));
                                                        LearningMapStudent learningMapStudent10 = new LearningMapStudent(idmUser, organization, learningMap10, date, true);
                                                        genericDaoSupport.save(learningMapStudent10);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("设计")) {
                                                        LearningMap learningMap3 = genericDaoSupport.load(LearningMap.class, new Long(3));
                                                        LearningMapStudent learningMapStudent3 = new LearningMapStudent(idmUser, organization, learningMap3, date, true);
                                                        genericDaoSupport.save(learningMapStudent3);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("投资")) {
                                                        LearningMap learningMap2 = genericDaoSupport.load(LearningMap.class, new Long(2));
                                                        LearningMapStudent learningMapStudent2 = new LearningMapStudent(idmUser, organization, learningMap2, date, true);
                                                        genericDaoSupport.save(learningMapStudent2);
                                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("运营")) {
                                                        LearningMap learningMap4 = genericDaoSupport.load(LearningMap.class, new Long(4));
                                                        LearningMapStudent learningMapStudent4 = new LearningMapStudent(idmUser, organization, learningMap4, date, true);
                                                        genericDaoSupport.save(learningMapStudent4);
                                                    }
                                                }
                                            }
                                        }
                                    } else {

                                        // TODO add error log here
                                        String message = "";

                                        if (idmUser.getUsertype() == null) {
                                            message = message + "用户类型为空;";
                                        }

                                        if (!idmUser.getUsertype().equalsIgnoreCase("E1") && !idmUser.getUsertype().equalsIgnoreCase("E2")) {
                                            message = message + "用户类型【" + idmUser.getUsertype() + "】不为E1或E2;";
                                        }

                                        if (StringUtils.isNotBlank(idmUser.getZ9121znt())) {
                                            message = message + "用户职位为空;";
                                        }

                                        if (StringUtils.isBlank(idmUser.getDutyid())) {
                                            message = message + "用户职级为空;";
                                        } else {
                                            try {
                                                new BigDecimal(idmUser.getDutyid());
                                            } catch (Exception e) {
                                                message = message + "用户职级" + idmUser.getDutyid() + "非数字类型;";
                                            }
                                        }

                                        IdmSyncLog idmSyncLog = new IdmSyncLog(idmUser, false, message, JsonUtils.toJson(idmUser));
                                        genericDaoSupport.save(idmSyncLog);

                                    }
                                }
                            }

                        }
                    }
                }
            }

            result.success().message("update idm_user success");

        } catch (UnsupportedEncodingException e) {
            logger.error("#updateUser error - ", e);
            result.fail("500").message("update user failed");
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#createUserLearningMap(java.lang.String)
     */
    public void createUserLearningMap(String dateString) {

        List<IdmUser> users = idmUserDao.lisyAlivaleUserByDate(dateString);
        List<Organization> organizations = genericDaoSupport.loadAll(Organization.class);
        Map<String, Organization> organizationsMap = CollectionUtils.constructMapValue(organizations, "orgnofullpath");

        for (IdmUser idmUser : users) {
            if (idmUser.getMail() != null && idmUser.getUsertype() != null && StringUtils.isNotEmpty(idmUser.getDutyid()) && StringUtils.isNotBlank(idmUser.getZ9121znt()) && (idmUser.getUsertype().equalsIgnoreCase("E1") || idmUser.getUsertype().equalsIgnoreCase("E2"))) {

                // start create learning map
                logger.info("current new user is [{}]", idmUser.getMail());
                logger.info("current new user duty is [{}]", idmUser.getDutyid());

                Date date = null;
                if (StringUtils.isNotEmpty(idmUser.getDutyid())) {
                    BigDecimal duity = null;
                    try {
                        duity = new BigDecimal(idmUser.getDutyid());
                    } catch (Exception e) {
                        logger.info("current new user duty is not number, ignore here", idmUser.getDutyid());
                        continue;
                    }

                    if (duity.compareTo(new BigDecimal("6.0")) < 0) {
                        date = new Date();
                        date = DateUtils.addMonths(date, 3);
                    } else {
                        date = new Date();
                        date = DateUtils.addMonths(date, 6);
                    }
                }

                logger.info("current new user parent org is [{}]", idmUser.getParentOrgnofullpath());
                if (idmUser.getParentOrgnofullpath() != null) {

                    Organization organization = organizationsMap.get(idmUser.getParentOrgnofullpath());
                    if (organization != null) {

                        LearningMapStudent learningMapStudent1 = idmUserDao.load(idmUser.getMail(), new Long(1));
                        if (learningMapStudent1 == null) {
                            LearningMap learningMap1 = genericDaoSupport.load(LearningMap.class, new Long(1));
                            learningMapStudent1 = new LearningMapStudent(idmUser, organization, learningMap1, date, true);
                            genericDaoSupport.save(learningMapStudent1);
                        }

                        if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("财务")) {

                            LearningMapStudent learningMapStudent8 = idmUserDao.load(idmUser.getMail(), new Long(8));
                            if (learningMapStudent8 == null) {
                                LearningMap learningMap8 = genericDaoSupport.load(LearningMap.class, new Long(8));
                                learningMapStudent8 = new LearningMapStudent(idmUser, organization, learningMap8, date, true);
                                genericDaoSupport.save(learningMapStudent8);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("采购")) {

                            LearningMapStudent learningMapStudent6 = idmUserDao.load(idmUser.getMail(), new Long(6));
                            if (learningMapStudent6 == null) {
                                LearningMap learningMap6 = genericDaoSupport.load(LearningMap.class, new Long(6));
                                learningMapStudent6 = new LearningMapStudent(idmUser, organization, learningMap6, date, true);
                                genericDaoSupport.save(learningMapStudent6);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("成本")) {

                            LearningMapStudent learningMapStudent7 = idmUserDao.load(idmUser.getMail(), new Long(7));
                            if (learningMapStudent7 == null) {
                                LearningMap learningMap7 = genericDaoSupport.load(LearningMap.class, new Long(7));
                                learningMapStudent7 = new LearningMapStudent(idmUser, organization, learningMap7, date, true);
                                genericDaoSupport.save(learningMapStudent7);
                            }
                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("法务")) {

                            LearningMapStudent learningMapStudent11 = idmUserDao.load(idmUser.getMail(), new Long(11));
                            if (learningMapStudent11 == null) {
                                LearningMap learningMap11 = genericDaoSupport.load(LearningMap.class, new Long(11));
                                learningMapStudent11 = new LearningMapStudent(idmUser, organization, learningMap11, date, true);
                                genericDaoSupport.save(learningMapStudent11);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("工程")) {

                            LearningMapStudent learningMapStudent5 = idmUserDao.load(idmUser.getMail(), new Long(5));
                            if (learningMapStudent5 == null) {
                                LearningMap learningMap5 = genericDaoSupport.load(LearningMap.class, new Long(5));
                                learningMapStudent5 = new LearningMapStudent(idmUser, organization, learningMap5, date, true);
                                genericDaoSupport.save(learningMapStudent5);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("行政") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("人力资源") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("学院")) {

                            LearningMapStudent learningMapStudent9 = idmUserDao.load(idmUser.getMail(), new Long(9));
                            if (learningMapStudent9 == null) {
                                LearningMap learningMap9 = genericDaoSupport.load(LearningMap.class, new Long(9));
                                learningMapStudent9 = new LearningMapStudent(idmUser, organization, learningMap9, date, true);
                                genericDaoSupport.save(learningMapStudent9);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("客户关系")) {

                            LearningMapStudent learningMapStudent10 = idmUserDao.load(idmUser.getMail(), new Long(10));
                            if (learningMapStudent10 == null) {
                                LearningMap learningMap10 = genericDaoSupport.load(LearningMap.class, new Long(10));
                                learningMapStudent10 = new LearningMapStudent(idmUser, organization, learningMap10, date, true);
                                genericDaoSupport.save(learningMapStudent10);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("设计")) {

                            LearningMapStudent learningMapStudent3 = idmUserDao.load(idmUser.getMail(), new Long(3));
                            if (learningMapStudent3 == null) {
                                LearningMap learningMap3 = genericDaoSupport.load(LearningMap.class, new Long(3));
                                learningMapStudent3 = new LearningMapStudent(idmUser, organization, learningMap3, date, true);
                                genericDaoSupport.save(learningMapStudent3);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("投资")) {

                            LearningMapStudent learningMapStudent2 = idmUserDao.load(idmUser.getMail(), new Long(2));
                            if (learningMapStudent2 == null) {
                                LearningMap learningMap2 = genericDaoSupport.load(LearningMap.class, new Long(2));
                                learningMapStudent2 = new LearningMapStudent(idmUser, organization, learningMap2, date, true);
                                genericDaoSupport.save(learningMapStudent2);
                            }

                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("运营")) {

                            LearningMapStudent learningMapStudent4 = idmUserDao.load(idmUser.getMail(), new Long(4));
                            if (learningMapStudent4 == null) {
                                LearningMap learningMap4 = genericDaoSupport.load(LearningMap.class, new Long(4));
                                learningMapStudent4 = new LearningMapStudent(idmUser, organization, learningMap4, date, true);
                                genericDaoSupport.save(learningMapStudent4);
                            }

                        }
                    }
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#updateUser()
     */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult updateUserByJob() {
        HttpResult result = new HttpResult();

        // 1. load 3 ours late update user info;
        Date now = new Date();
        Date filterDate = org.apache.commons.lang.time.DateUtils.addMinutes(now, -90);
        String dateString = DateFormatUtils.format(filterDate, "yyyyMMddHHmmSS") + "Z";
        String filter = "(|(modifytimestamp>=" + dateString + ")(usercreatetime>=" + dateString + "))";
        logger.info("filter is [{}]", filter);
        List<IdmUser> idmUsers = idmHandler.listIdmUser(filter);

        for (IdmUser idmUser : idmUsers) {
            if (idmUser.getMail() != null) {
                IdmUser old = idmUserDao.load(idmUser.getMail());
                if (old != null) {
                    logger.info("current old user is [{}]", idmUser.getMail());
                    genericDaoSupport.update(old.update(idmUser));
                } else {

                    if (idmUser.getUsertype() != null && (idmUser.getUsertype().equalsIgnoreCase("E1") || idmUser.getUsertype().equalsIgnoreCase("E2"))) {
                        idmUser.editOrgnofullpath(idmUser.getOrgParent());
                        genericDaoSupport.save(idmUser);
                        logger.info("current new user is [{}]", idmUser.getMail());
                        // new user save leadship_map

                        Date date = null;
                        logger.info("current new user duty is [{}]", idmUser.getDutyid());
                        if (StringUtils.isNotEmpty(idmUser.getDutyid())) {
                            BigDecimal duity = null;
                            try {
                                duity = new BigDecimal(idmUser.getDutyid());
                            } catch (Exception e) {
                                logger.info("current new user duty is not number, ignore here", idmUser.getDutyid());
                                continue;
                            }
                            if (duity.compareTo(new BigDecimal("6.0")) < 0) {
                                date = new Date();
                                date = DateUtils.addMonths(date, 3);
                            } else {
                                date = new Date();
                                date = DateUtils.addMonths(date, 6);
                            }
                        }

                        logger.info("current new user parent org is [{}]", idmUser.getParentOrgnofullpath());
                        if (idmUser.getParentOrgnofullpath() != null) {
                            Organization organization = idmUserDao.loadOrganization(idmUser.getParentOrgnofullpath());
                            if (organization != null) {
                                LearningMap learningMap1 = genericDaoSupport.load(LearningMap.class, new Long(1));
                                LearningMapStudent learningMapStudent1 = new LearningMapStudent(idmUser, organization, learningMap1, date, true);
                                genericDaoSupport.save(learningMapStudent1);

                                String job = idmUser.getZ9121znt();
                                if (StringUtils.isNotBlank(job)) {
                                    if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("财务")) {
                                        LearningMap learningMap8 = genericDaoSupport.load(LearningMap.class, new Long(8));
                                        LearningMapStudent learningMapStudent8 = new LearningMapStudent(idmUser, organization, learningMap8, date, true);
                                        genericDaoSupport.save(learningMapStudent8);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("采购")) {
                                        LearningMap learningMap6 = genericDaoSupport.load(LearningMap.class, new Long(6));
                                        LearningMapStudent learningMapStudent6 = new LearningMapStudent(idmUser, organization, learningMap6, date, true);
                                        genericDaoSupport.save(learningMapStudent6);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("成本")) {
                                        LearningMap learningMap7 = genericDaoSupport.load(LearningMap.class, new Long(7));
                                        LearningMapStudent learningMapStudent7 = new LearningMapStudent(idmUser, organization, learningMap7, date, true);
                                        genericDaoSupport.save(learningMapStudent7);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("法务")) {
                                        LearningMap learningMap11 = genericDaoSupport.load(LearningMap.class, new Long(11));
                                        LearningMapStudent learningMapStudent11 = new LearningMapStudent(idmUser, organization, learningMap11, date, true);
                                        genericDaoSupport.save(learningMapStudent11);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("工程")) {
                                        LearningMap learningMap5 = genericDaoSupport.load(LearningMap.class, new Long(5));
                                        LearningMapStudent learningMapStudent5 = new LearningMapStudent(idmUser, organization, learningMap5, date, true);
                                        genericDaoSupport.save(learningMapStudent5);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("行政") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("人力资源") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("学院")) {
                                        LearningMap learningMap9 = genericDaoSupport.load(LearningMap.class, new Long(9));
                                        LearningMapStudent learningMapStudent9 = new LearningMapStudent(idmUser, organization, learningMap9, date, true);
                                        genericDaoSupport.save(learningMapStudent9);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("客户关系")) {
                                        LearningMap learningMap10 = genericDaoSupport.load(LearningMap.class, new Long(10));
                                        LearningMapStudent learningMapStudent10 = new LearningMapStudent(idmUser, organization, learningMap10, date, true);
                                        genericDaoSupport.save(learningMapStudent10);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("设计")) {
                                        LearningMap learningMap3 = genericDaoSupport.load(LearningMap.class, new Long(3));
                                        LearningMapStudent learningMapStudent3 = new LearningMapStudent(idmUser, organization, learningMap3, date, true);
                                        genericDaoSupport.save(learningMapStudent3);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("投资")) {
                                        LearningMap learningMap2 = genericDaoSupport.load(LearningMap.class, new Long(2));
                                        LearningMapStudent learningMapStudent2 = new LearningMapStudent(idmUser, organization, learningMap2, date, true);
                                        genericDaoSupport.save(learningMapStudent2);
                                    } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("运营")) {
                                        LearningMap learningMap4 = genericDaoSupport.load(LearningMap.class, new Long(4));
                                        LearningMapStudent learningMapStudent4 = new LearningMapStudent(idmUser, organization, learningMap4, date, true);
                                        genericDaoSupport.save(learningMapStudent4);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        result.success().message("update idm_user success");
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#updateUserWithNoNotification(java.lang.String)
     */
    public HttpResult updateUserWithNoNotification(String filter) {

        HttpResult result = new HttpResult();

        // 1. load 3 ours late update user info;
        logger.info("filter is [{}]", filter);
        List<IdmUser> idmUsers = idmHandler.listIdmUser(filter);

        for (IdmUser idmUser : idmUsers) {
            if (idmUser.getMail() != null) {
                IdmUser old = idmUserDao.load(idmUser.getMail());
                if (old != null) {
                    logger.info("current old user is [{}]", idmUser.getMail());
                    genericDaoSupport.update(old.update(idmUser));
                } else {

                    if (idmUser.getUsertype() != null) {
                        idmUser.editOrgnofullpath(idmUser.getOrgParent());
                        genericDaoSupport.save(idmUser);
                        logger.info("current new user is [{}]", idmUser.getMail());
                        // new user save leadship_map
                    }
                }
            }
        }

        result.success().message("update idm_user success");
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#updateUser(java.lang.String)
     */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult updateUser(String filter) {

        HttpResult result = new HttpResult();

        try {

            // 1. load 3 ours late update user info;
            logger.info("filter is [{}]", filter);
            List<IdmUser> idmUsers = idmHandler.listIdmUser(filter);

            for (IdmUser idmUser : idmUsers) {

                if (idmUser.getMail() != null) {
                    IdmUser old = idmUserDao.load(idmUser.getMail());
                    if (old != null) {
                        logger.info("current old user is [{}]", idmUser.getMail());
                        genericDaoSupport.update(old.update(idmUser));
                        throw BusinessException.newInstance("500", "同步用户已存在");
                    } else {
                        if (idmUser.getUsertype() != null && (idmUser.getUsertype().equalsIgnoreCase("E1") || idmUser.getUsertype().equalsIgnoreCase("E2"))) {
                            
                            idmUser.editOrgnofullpath(idmUser.getOrgParent());
                            genericDaoSupport.save(idmUser);
                            logger.info("current new user is [{}]", idmUser.getMail());
                            // new user save leadship_map

                            Date date = null;
                            logger.info("current new user duty is [{}]", idmUser.getDutyid());
                            if (StringUtils.isNotEmpty(idmUser.getDutyid())) {
                                BigDecimal duity = null;
                                try {
                                    duity = new BigDecimal(idmUser.getDutyid());
                                } catch (Exception e) {
                                    logger.info("current new user duty is not number, ignore here", idmUser.getDutyid());
                                    throw BusinessException.newInstance("500", "同步用户职级不存在");
                                }
                                if (duity.compareTo(new BigDecimal("6.0")) < 0) {
                                    date = new Date();
                                    date = DateUtils.addMonths(date, 3);
                                } else {
                                    date = new Date();
                                    date = DateUtils.addMonths(date, 6);
                                }
                            } else {
                                throw BusinessException.newInstance("500", "同步用户职级不存在");
                            }

                            logger.info("current new user parent org is [{}]", idmUser.getParentOrgnofullpath());
                            if (idmUser.getParentOrgnofullpath() != null) {
                                Organization organization = idmUserDao.loadOrganization(idmUser.getParentOrgnofullpath());
                                if (organization != null) {

                                    LearningMap learningMap1 = genericDaoSupport.load(LearningMap.class, new Long(1));
                                    LearningMapStudent learningMapStudent1 = new LearningMapStudent(idmUser, organization, learningMap1, date, true);
                                    genericDaoSupport.save(learningMapStudent1);

                                    String job = idmUser.getZ9121znt();
                                    if (StringUtils.isNotBlank(job)) {
                                        if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("财务")) {
                                            LearningMap learningMap8 = genericDaoSupport.load(LearningMap.class, new Long(8));
                                            LearningMapStudent learningMapStudent8 = new LearningMapStudent(idmUser, organization, learningMap8, date, true);
                                            genericDaoSupport.save(learningMapStudent8);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("采购")) {
                                            LearningMap learningMap6 = genericDaoSupport.load(LearningMap.class, new Long(6));
                                            LearningMapStudent learningMapStudent6 = new LearningMapStudent(idmUser, organization, learningMap6, date, true);
                                            genericDaoSupport.save(learningMapStudent6);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("成本")) {
                                            LearningMap learningMap7 = genericDaoSupport.load(LearningMap.class, new Long(7));
                                            LearningMapStudent learningMapStudent7 = new LearningMapStudent(idmUser, organization, learningMap7, date, true);
                                            genericDaoSupport.save(learningMapStudent7);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("法务")) {
                                            LearningMap learningMap11 = genericDaoSupport.load(LearningMap.class, new Long(11));
                                            LearningMapStudent learningMapStudent11 = new LearningMapStudent(idmUser, organization, learningMap11, date, true);
                                            genericDaoSupport.save(learningMapStudent11);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("工程")) {
                                            LearningMap learningMap5 = genericDaoSupport.load(LearningMap.class, new Long(5));
                                            LearningMapStudent learningMapStudent5 = new LearningMapStudent(idmUser, organization, learningMap5, date, true);
                                            genericDaoSupport.save(learningMapStudent5);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("行政") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("人力资源") || idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("学院")) {
                                            LearningMap learningMap9 = genericDaoSupport.load(LearningMap.class, new Long(9));
                                            LearningMapStudent learningMapStudent9 = new LearningMapStudent(idmUser, organization, learningMap9, date, true);
                                            genericDaoSupport.save(learningMapStudent9);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("客户关系")) {
                                            LearningMap learningMap10 = genericDaoSupport.load(LearningMap.class, new Long(10));
                                            LearningMapStudent learningMapStudent10 = new LearningMapStudent(idmUser, organization, learningMap10, date, true);
                                            genericDaoSupport.save(learningMapStudent10);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("设计")) {
                                            LearningMap learningMap3 = genericDaoSupport.load(LearningMap.class, new Long(3));
                                            LearningMapStudent learningMapStudent3 = new LearningMapStudent(idmUser, organization, learningMap3, date, true);
                                            genericDaoSupport.save(learningMapStudent3);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("投资")) {
                                            LearningMap learningMap2 = genericDaoSupport.load(LearningMap.class, new Long(2));
                                            LearningMapStudent learningMapStudent2 = new LearningMapStudent(idmUser, organization, learningMap2, date, true);
                                            genericDaoSupport.save(learningMapStudent2);
                                        } else if (idmUser.getZ9121znt() != null && idmUser.getZ9121znt().contains("运营")) {
                                            LearningMap learningMap4 = genericDaoSupport.load(LearningMap.class, new Long(4));
                                            LearningMapStudent learningMapStudent4 = new LearningMapStudent(idmUser, organization, learningMap4, date, true);
                                            genericDaoSupport.save(learningMapStudent4);
                                        }
                                    }
                                }
                            } else {
                                throw BusinessException.newInstance("500", "同步所属组织不存在");
                            }
                        } else {
                            throw BusinessException.newInstance("500", "同步用户信息缺失，请检查");
                        }
                    }
                }
            }
            
            result.success().message("update idm_user success");
            
        } catch (Exception e) {
            result.fail("500").message(e.getMessage());
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#noticeLearningMap()
     */
    @SuppressWarnings("unchecked")
    public HttpResult noticeLearningMap() {

        HttpResult result = new HttpResult();

        try {
            String sentence = "FROM LearningMapStudent WHERE disabled = false AND needNotice = true";

            List<LearningMapStudent> learningMapStudents = genericDaoSupport.searchForList(sentence);

            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(learningMapStudents)) {

                Map<String, List<LearningMapStudent>> learningMapStudentMaps = CollectionUtils.constructMapValues(learningMapStudents, "learningMap.id");
                for (String key : learningMapStudentMaps.keySet()) {

                    List<LearningMapStudent> currentLearningStudent = learningMapStudentMaps.get(key);
                    String targets = "";
                    Map<String, String> parentTargets = new HashMap<String, String>();
                    for (LearningMapStudent learningMapStudent : currentLearningStudent) {
                        targets = targets + learningMapStudent.getEmail() + "|";
                        IdmUser student = idmUserDao.load(learningMapStudent.getEmail());
                        if (student != null && student.getZ9122hb() != null && student.getZ9122hb() != "") {
                            IdmUser parentIdmUser = idmUserDao.loadByNumber(student.getZ9122hb());
                            if (parentIdmUser != null) {
                                if (parentTargets.get(parentIdmUser.getMail()) == null) {
                                    parentTargets.put(parentIdmUser.getMail(), student.getUsercn());
                                } else {
                                    String usercn = parentTargets.get(parentIdmUser.getMail()) + "、" + student.getUsercn();
                                    parentTargets.put(parentIdmUser.getMail(), usercn);
                                }
                            }

                        }
                        learningMapStudent.setNeedNotice(false);
                        genericDaoSupport.update(learningMapStudent);
                    }

                    this.sendDingMessage(currentLearningStudent.get(0).getLearningMap(), currentLearningStudent.get(0).getLearningMap().getName(), null, targets);
                    this.sendPreantMessage(parentTargets, currentLearningStudent.get(0).getLearningMap(), null);
                }

                result.success().message("通知成功");

            } else {
                result.success().message("暂无学习地图需要通知");
            }

        } catch (Exception e) {
            logger.error("send notice errot");
            result.fail("5000").message("发生错误");
        }

        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#noticeLearningMap()
     */
    @SuppressWarnings("unchecked")
    public HttpResult statLearningMap() {

        HttpResult result = new HttpResult();

        try {
            String sentence = "FROM LearningMapStudent WHERE disabled = false AND needNotice = false";

            List<LearningMapStudent> learningMapStudents = genericDaoSupport.searchForList(sentence);
            for (LearningMapStudent learningMapStudent : learningMapStudents) {
                logger.info("current learningmap student is [{}] [{}]", learningMapStudent.getEmail(), learningMapStudent.getLearningMap().getId());
                UMUCourseStat stat = umuHandler.courseStat(learningMapStudent);
                if (stat != null) {
                    logger.info("current learningmap student  stat is [{}]", JsonUtils.toJson(stat));
                    learningMapStudent.updateGrade(stat.getOverallCompletionRate(), stat.getRequiredCompletionRate());
                    genericDaoSupport.update(learningMapStudent);
                }
            }

            result.success().message("更新学员地图完成率成功");

        } catch (Exception e) {
            logger.error("send notice errot");
            result.fail("5000").message("发生错误");
        }

        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.IdmService#updateUser(java.lang.String)
     */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult updateOrgUser(String orgNo) throws UnsupportedEncodingException {
        HttpResult result = new HttpResult();

        List<IdmOrg> idmOrgs = idmDao.listIdmOrg(orgNo + "%");
        for (IdmOrg idmOrg : idmOrgs) {
            // 4. get user by orgnofullpath
            String encode = URLEncoder.encode(idmOrg.getOrgnofullpath(), "UTF-8");
            String filter = "(orgnofullpath=" + encode + ")";
            // 1. load 3 ours late update user info;
            logger.info("filter is [{}]", filter);

            List<IdmUser> idmUsers = idmHandler.listIdmUser(filter);

            for (IdmUser idmUser : idmUsers) {
                if (StringUtils.isNotBlank(idmUser.getMail())) {
                    IdmUser old = idmUserDao.load(idmUser.getMail());
                    if (old != null) {
                        logger.info("current old user is [{}]", idmUser.getMail());
                        genericDaoSupport.update(old.update(idmUser));
                    } else {

                        if (idmUser.getUsertype() != null && (idmUser.getUsertype().equalsIgnoreCase("E1") || idmUser.getUsertype().equalsIgnoreCase("E2"))) {
                            idmUser.editOrgnofullpath(idmUser.getOrgParent());
                            genericDaoSupport.save(idmUser);
                            logger.info("current new user is [{}]", idmUser.getMail());

                            Date date = null;
                            logger.info("current new user duty is [{}]", idmUser.getDutyid());
                            if (StringUtils.isNotEmpty(idmUser.getDutyid())) {
                                BigDecimal duity = null;
                                try {
                                    duity = new BigDecimal(idmUser.getDutyid());
                                } catch (Exception e) {
                                    logger.info("current new user duty is not number, ignore here", idmUser.getDutyid());
                                    continue;
                                }
                                if (duity.compareTo(new BigDecimal("6.0")) < 0) {
                                    date = new Date();
                                    date = DateUtils.addMonths(date, 3);
                                } else {
                                    date = new Date();
                                    date = DateUtils.addMonths(date, 6);
                                }
                            }
                        }
                    }
                }
            }

        }
        result.success().message("update idm_user success");
        return result;
    }

    @SuppressWarnings("deprecation")
    private void sendDingMessage(LearningMap learningMap, String learningName, Date endTime, String targets) {

        String url = "https://passport.umu.cn/sso/login?from=dingtalk&name=xuhui&redirect_url=" + learningMap.getLink();
        String pcUrl = "https://passport.umu.cn/oauth/cifi/login?redirect_url=" + URLDecoder.decode(learningMap.getLink());

        // 钉钉 message
        String message = "[![ding-banner](http://aipunet.qiniu.edooschool.com/ding-banner.jpg)](" + url + ") \n   \n   \n ### 您收到一个新的学习任务";
        message = message + " \n  \n  任务名称：" + learningMap.getName() + "";

        if (endTime != null) {
            message = message + " \n  \n  截止时间：" + DateFormatUtils.format(endTime, "yyyy-MM-dd") + "";
        }
        message = message + " \n  \n  重要提醒：请在【手机端】钉钉消息打开";
        message = message + " \n \n \n [<font color=#FF0000>点击此处开启您的学习旅程吧！</font>](" + url + ")";

        // Email message
        String emailMessage = "您收到一个新的学习地图|您好！您在旭辉学习管理系统（CLMS）里收到了";
        if (learningMap.getId().equals(1L)) {
            emailMessage = emailMessage + "一个新员工通用学习地图。<br>您需要在转正前完成《新员工学习融入计划》，帮助您充分了解企业文化和公司概况，熟悉基本管理制度和流程，快速融入旭辉，发挥岗位价值。";
        } else {
            emailMessage = emailMessage + "本条线的新员工专业学习地图。<br>您需要在转正前完成由本条线与旭辉学院共同设计开发的《" + learningName + "》，了解所在条线的管控体系和管理标准，把握工作方向，快速融入，发挥岗位价值。";
        }
        emailMessage = emailMessage + "<br>任务名称：" + learningMap.getName() + "<br>";
        if (endTime != null) {
            emailMessage = emailMessage + "截止时间：" + DateFormatUtils.format(endTime, "yyyy-MM-dd") + "<br>";
        }
        emailMessage = emailMessage + "<a href=" + pcUrl + ">点击此处开启您的学习旅程吧！</a><br>";

        if (!targets.equalsIgnoreCase("")) {
            targets = targets.substring(0, targets.length() - 1);

            Map<String, Object> params = new HashMap<String, Object>();

            params.put("userId", targets);
            params.put("message", message);
            HttpResult httpResult = surveyRao.sendDingMessage("http://leadership.demo2do.com/leadership-website/api/ding-message", params);
            if (!httpResult.isValid()) {
                logger.error("send ding message error response [{}]", JsonUtils.toJson(httpResult));
                // throw BusinessException.newInstance("500", "发送钉钉消息失败");
            }

            Map<String, Object> params2 = new HashMap<String, Object>();
            params2.put("userId", targets + "|809107193@qq.com");
            params2.put("message", emailMessage);
            HttpResult httpResult2 = surveyRao.sendDingEmail("http://leadership.demo2do.com/leadership-website/api/ding-email", params2);
            if (!httpResult2.isValid()) {
                // throw BusinessException.newInstance("500", "发送邮件失败");
                logger.error("send ding email error response [{}]", JsonUtils.toJson(httpResult2));
            }
        }
    }

    private void sendPreantMessage(Map<String, String> parentTargets, LearningMap learningMap, Date endTime) {
        String url = "https://passport.umu.cn/sso/login?from=dingtalk&name=xuhui&redirect_url=" + learningMap.getLink();

        String message1 = "[![ding-banner](http://aipunet.qiniu.edooschool.com/ding-banner.jpg)](" + url + ") \n   \n   \n 您部门的伙伴";
        String emailMessage1 = "您部门的伙伴接收到一份新的学习任务|您好！您部门的伙伴";

        Map<String, String> parentMessages = new HashMap<String, String>();
        Map<String, String> parentEmailMessages = new HashMap<String, String>();

        for (String key : parentTargets.keySet()) {
            String partners = parentTargets.get(key);
            String parentMessage = message1 + partners + "进入" + learningMap.getName() + "开始学习啦！";
            if (endTime != null) {
                parentMessage = parentMessage + " \n  \n 截止时间：" + DateFormatUtils.format(endTime, "yyyy.MM.dd") + " ";
            }

            parentMessages.put(key, parentMessage);
            String parentEmailMessage = emailMessage1 + partners + "接收到" + learningMap.getName() + "，并开始学习啦！" + "<br>";
            if (endTime != null) {
                parentEmailMessage = parentEmailMessage + "截止时间：" + DateFormatUtils.format(endTime, "yyyy.MM.dd") + "<br>";
            }
            parentEmailMessage = parentEmailMessage + "期待与您同奋斗·共成长！";
            parentEmailMessages.put(key, parentEmailMessage);
        }

        if (!parentMessages.isEmpty()) {
            for (String key : parentMessages.keySet()) {
                Map<String, Object> parentparams = new HashMap<String, Object>();
                parentparams.put("userId", key);
                parentparams.put("message", parentMessages.get(key));
                logger.info("send ding to parent [{}]", parentEmailMessages.get(key));
                HttpResult parenthttpResult = surveyRao.sendDingMessage("http://leadership.demo2do.com/leadership-website/api/ding-message", parentparams);
                if (!parenthttpResult.isValid()) {
                    throw BusinessException.newInstance("500", "发送钉钉消息失败");
                }
            }

        }

        if (!parentEmailMessages.isEmpty()) {
            for (String key : parentEmailMessages.keySet()) {
                Map<String, Object> parentEmailParams = new HashMap<String, Object>();
                parentEmailParams.put("userId", key + "|809107193@qq.com");
                parentEmailParams.put("message", parentEmailMessages.get(key));
                logger.info("send email to parent [{}]", parentEmailMessages.get(key));
                HttpResult parentEmailHttpResult = surveyRao.sendDingEmail("http://leadership.demo2do.com/leadership-website/api/ding-email", parentEmailParams);
                if (!parentEmailHttpResult.isValid()) {
                    throw BusinessException.newInstance("500", "发送邮件失败");
                }
            }
        }
    }

}
